package com.example.store2.Service.impl;

import com.example.store2.Mapper.BeAddMapper;
import com.example.store2.Mapper.UserMapper;
import com.example.store2.Service.UserService;
import com.example.store2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BeAddMapper beAddMapper;
    /**
     * 显示用户信息
     * @param username
     * @return
     */
    @Override
    public User show(String username) {
        return userMapper.show(username);
    }
    /**
     * 修改用户信息
     * @param user
     */
    @Override
    public boolean update(User user) {
        //进行升级
        int NowJurisdiction=userMapper.getbyid(user.getId());
        if(user.getJurisdiction()!=NowJurisdiction) {
            int num=beAddMapper.count(user.getId());
            if(num>=NowJurisdiction*20){
                userMapper.update(user);
                return true;
            }
            else
                return false;
        }
        user.setUpdateTime(LocalDateTime.now());//更新修改时间
        if(user.getName()==null || user.getName().equals(""))
            return false;
        else{
            userMapper.update(user);
            return true;
        }
    }

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public boolean regiser(User user) {
        if(userMapper.find(user) != null)
            return false;
        else{
            userMapper.add(user);
            return true;
        }
    }


}
